package com.wan.service.system;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wan.config.StaticImageConfig;
import com.wan.entity.CollageInfo;
import com.wan.entity.GameRecord;
import com.wan.entity.OfficeBuild;
import com.wan.mapper.OfficeBuildMapper;
import com.wan.service.GameRecordService;
import entity.ResultBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import utils.TimeUtils;
import utils.UUIDUtils;

import java.io.File;
import java.io.IOException;

/**
 * <p>
 * 办公楼信息表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2020-03-11
 */
@Slf4j
@Service
public class OfficeBuildingService extends ServiceImpl<OfficeBuildMapper, OfficeBuild> {

    private static final String SEPARATOR = "/";
    private static final long FILE_SIZE = 1024*1024;


    @Autowired
    private StaticImageConfig staticImageConfig;
    @Autowired
    private CollageService collageService;
    @Autowired
    private PositionService positionService;
    @Autowired
    private GameRecordService gameRecordService;

    @Value("${map.url}")
    private String url;

    public ResultBean fileUpload(MultipartFile file) {
        if (file == null) {
            return new ResultBean(500,"图片不能为空");
        }
        if (file.getSize() == 0 ){
            return new ResultBean(500,"图片不能为空");
        }
        if (file.getSize() > FILE_SIZE){
            return new ResultBean(500,"图片大小不能超过1M");
        }
        String uuid = UUIDUtils.getUUID();
        String fileName = file.getOriginalFilename();
        log.info("上传的文件名{}==>"+fileName);
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        String today = TimeUtils.GetNowDate("yyyy-MM-dd");
        String path = staticImageConfig.getUploadPath();
        //存储文件到磁盘
        StringBuilder dictPath = new StringBuilder();
        dictPath.append(path).append(today).append(SEPARATOR).append(uuid).append(suffix);

        File dictFile = new File(dictPath.toString());
        if(!dictFile.getParentFile().exists()) {
            dictFile.getParentFile().mkdirs();
        }

        try {
            file.transferTo(dictFile);
        } catch (IOException e) {
            e.printStackTrace();
            return new ResultBean(500,"文件上传失败");
        }
        JSONObject jsonObject = new JSONObject();
        String src = url+today+SEPARATOR+uuid+suffix;
        jsonObject.put("src",src);

        return new ResultBean(0,"文件上传成功",jsonObject);
    }

    @Transactional(rollbackFor = Exception.class)
    public String officeBuildSave(GameRecord record) {
        record.setCreateTime(TimeUtils.GetNowDate("yyyy-MM-dd HH:mm:ss"));
        Boolean b = gameRecordService.save(record);
        if (!b){
            return "添加失败";
        }else {
            return "添加成功";
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public ResultBean updateOffice(Integer id, String officeBuildName, String imageFile, String detiledDescription, String anchor,Integer collageId) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        OfficeBuild officeBuild = new OfficeBuild();
        CollageInfo collageInfo = collageService.getById(collageId);
        officeBuild.setId(id);
        officeBuild.setUsername(authentication.getName());
        officeBuild.setAnchor(anchor);
        officeBuild.setDetiledDescription(detiledDescription);
        String url = imageFile.substring(21);
        officeBuild.setImgUrl(url);
        //officeBuild.setImgUrl(imageFile);
        officeBuild.setCollageId(collageId);
        officeBuild.setCollageName(collageInfo.getCollageName());
        officeBuild.setOfficeBuildingName(officeBuildName);
        officeBuild.setUpdateTime(TimeUtils.GetNowDate("yyyy-MM-dd HH:mm:ss"));
        Integer i = this.baseMapper.updateById(officeBuild);
        if (i == null){
            return new ResultBean(500,"办公楼更新失败");
        }else {
            return new ResultBean(200,"办公楼更新成功");
        }
    }
}
